package com.ruoyi.framework.aspectj;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.DataVerification;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.annotation.RateLimiter;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.http.HttpClients;
import com.ruoyi.system.domain.SysDataCheckLog;
import com.ruoyi.system.mapper.SysDataCheckLogMapper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @Author kingwin
 * @Date 2022/5/17 21:46
 */
@Aspect
@Component
public class DataVerificationAspect {

    @Resource
    SysDataCheckLogMapper logMapper;

    private static final Logger log = LoggerFactory.getLogger(LogAspect.class);

    @Before("@annotation(dataVerification)")
    public void DataVerification(JoinPoint point, DataVerification dataVerification){

        Object[] args = point.getArgs();
        if (args.length > 0){
            String param = JSON.toJSONString(args[0]);
            JSONArray array = new JSONArray();
            array.add(param);
            Map<String, JSONArray> map = new HashMap<>();
            map.put("text_list",array);
            String params = JSON.toJSONString(map);
            String result = HttpClients.doPost("http://proof.sc/pc_database/batch_proof", params);
            JSONArray resultArray = JSONArray.parseArray(result);
            if (resultArray!=null && !resultArray.equals("[]")){
                JSONArray arr = (JSONArray) resultArray.get(0);
                if (arr!=null && !arr.equals("[]")) {
                    JSONArray o = (JSONArray) resultArray.get(0);
                    for (int i = 0;i< o.size();i++){
                        String jsonStr = JSON.toJSONString(o.getJSONObject(i));
                        SysDataCheckLog log = JSON.parseObject(jsonStr, SysDataCheckLog.class);
                        log.setName(dataVerification.name());
                        log.setCreateTime(new Date());
                        logMapper.insertSysDataCheckLog(log);
                        throw new RuntimeException("存在违规字符【" + log.getSource() + "】");
                    }
                }
            }
        }



//        [
//  [],
//  [
//        {
//            "source": "不记初心",
//                "term": "不忘初心",
//                "remark": null,
//                "pos": 0,
//                "end_pos": 4,
//                "error_type": "POLITICAL_TERM_TYPO",
//                "source_around": "不记初心",
//                "can_be_list": null,
//                "hint": "不忘初心",
//                "labels": [
//            "POLITICAL_MUST_RIGHT"
//      ],
//            "units": [
//            1
//      ],
//            "debug": null,
//                "label_copy": [
//            null
//      ]
//        }
//  ]
//]
    }

}
